package com.xiaochong.tir.manager.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.common.data.po.SyChannels;
import com.xiaochong.tir.manager.enums.StatusEnum;
import com.xiaochong.tir.manager.service.SyChannelsService;
import com.xiaochong.tir.manager.util.ResultEntity;
import com.xiaochong.tir.manager.util.ReturnResult;

/**   
 * @ClassName:  SyChannelsController   
 * @Description:TODO(频道管理)   
 * @author: maxinhong
 * @date:   2018年5月7日 下午5:01:27   
 */
@Controller
@RequestMapping("/manage/syChannels")
@Api("频道管理")
public class SyChannelsController {

	@Autowired
	private SyChannelsService syChannelsService;
	@Autowired
	private RedisServer redisServer;

	@RequestMapping(value = "/syChannelsList", method = RequestMethod.GET)
	public String syChannelsList() {
		return "/syChannels/syChannelsList";
	}
	
	@RequestMapping(value = "/addSyChannels", method = RequestMethod.GET)
	public String addSyChannels() {
		return "/syChannels/syChannelsAdd";
	}

	/**   
	 * @Title: listSyChannels   
	 * @Description: TODO(列表)   
	 * @param: @param find
	 * @param: @param page
	 * @param: @param limit
	 * @param: @return      
	 * @return: Object      
	 * @throws   
	 */
	@ApiOperation(value = "查询频道列表",notes = "查询频道列表")
	@RequestMapping(value = "/listSyChannels")
	@ResponseBody
	public Object listSyChannels(@RequestParam(value = "find", required = false) String find,
			@RequestParam("page") int page,@RequestParam("limit") int limit) {
		try {
			PageInfo<SyChannels> pageInfo = syChannelsService.getSyChannelsList(find,page, limit);
			return new ReturnResult<SyChannels>(0, "查询成功", pageInfo.getList(), pageInfo.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/listSyChannels", "[/manage/syChannels/listSyChannels error {}]",
					e);
		}
		return null;
	}

	
	/**   
	 * @Title: saveSyChannels   
	 * @Description: TODO(新增)   
	 * @param: @param syChannels
	 * @param: @param session
	 * @param: @param model
	 * @param: @return      
	 * @return: Object      
	 * @throws   
	 */
	@ApiOperation(value = "新增频道",notes = "新增频道")
	@RequestMapping("/saveSyChannels")
	public Object saveSyChannels(SyChannels syChannels) {
		redisServer.set(RedisKeysUtils.LOGINNAME,"新增频道");
		try {
			SyChannels channels = syChannelsService.selectByEntity(syChannels);
			if(null != channels){
				return "/syChannels/syChannelsList";
			}
			syChannels.setEnableFlag(true);
			syChannels.setDeleteFlag(false);
			syChannels.setCreateTime(new Date());
			syChannelsService.insertSelective(syChannels);
			return "/syChannels/syChannelsList";
		} catch (Exception e) {
			e.printStackTrace();
			LogTrace.error(getClass().getSimpleName() + "/saveSyChannels", e.getMessage(),e);
		}
		return null;
	}
	
	/**   
	 * @Title: delSyChannels   
	 * @Description: TODO(删除)   
	 * @param: @param id
	 * @param: @return      
	 * @return: ResultEntity<JSONObject>      
	 * @throws   
	 */
	@ApiOperation(value = "删除频道",notes = "删除频道")
	@GetMapping("/delSyChannels")
    @ResponseBody
    public ResultEntity<JSONObject> delSyChannels(@RequestParam("id") Integer id){
		redisServer.set(RedisKeysUtils.LOGINNAME,"删除频道");
        ResultEntity<JSONObject> result = new ResultEntity<>();
        try {
        	SyChannels syChannels = new SyChannels();
        	syChannels.setId(id);
        	syChannels.setDeleteFlag(true);
			syChannels.setEnableFlag(true);
        	syChannelsService.updateByPrimaryKey(syChannels,syChannelsService.selectByPrimaryKey(id));
            result.setStatus(StatusEnum.OK);
        } catch (Exception e) {
            result.setStatus(StatusEnum.SERVER_ERROR);
            LogTrace.error(getClass().getSimpleName() + "/delSyChannels", e.getMessage(),e);
        }
        return result;
    }
}
